<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id = "timer"></div>
</body>
<script>
    /**
     *在页面上生成动态时钟  2021-10-15 11:12:12 
     * 
     * 
     * 
     */

    //  function f(num){
    //     return num <10?"0"+num:num;
    // }
    
    // var index = setInterval(function time(){
    //     var date2 = new Date();
    //     var hour = date2.getHours();
    //     var minute = date2.getMinutes();
    //     var second = date2.getSeconds();

    //     document.write(date2.getFullYear() +"-"+(date2.getMonth()+1) +"-"+date2.getDate()+" "+f(hour)+":"+f(minute)+":"+f(second) + "<br>");
    //     second = second +1;
    // },1000)
    
    function f(num){
        return num <10?"0"+num:num;
    }

    function time(){
        var date2 = new Date();
        var hour = date2.getHours();
        var minute = date2.getMinutes();
        var second = date2.getSeconds();
        var resuly = date2.getFullYear() +"-"+(date2.getMonth()+1) +"-"+date2.getDate()+" "+f(hour)+":"+f(minute)+":"+f(second);
        return resuly;
    }
    var timed = setInterval(function(){
        var result = time();
        var timer = document.getElementById("timer");
        timer.innerHTML = result;
    },1000)
</script>
</html>